package inet.db.tennis;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import inet.db.pool.*;
import inet.util.Logger;

public class TennisCategoryDAO {

	private Logger logger = null;
	private DBPoolX poolX = null;
	public TennisCategoryDAO(){
		logger = new Logger(this.getClass().getName());
		try{
			poolX = DBPoolX.getInstance(DBPoolXName.SERVICE_BONGDA);
		}catch(Exception exception){
			logger.log(exception.toString()); 
		}
	}
	
	/**
	 * function get a TennisCategory by id
	 * @param id
	 * @return
	 */
    public TennisCategory getRow(BigDecimal id) {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        String strSQL = null;
        TennisCategory category = null;
        try {
            conn = poolX.getConnection();
            strSQL =
                "select ID, NAME, TYPE, GEN_DATE from TENNIS_CATEGORY where ID = ? ";
            preStmt = conn.prepareStatement(strSQL);
            preStmt.setBigDecimal(1, id);
            rs = preStmt.executeQuery();
            if (rs.next()) {
            	category = new TennisCategory();
            	category.setId(rs.getBigDecimal(1));
            	category.setName(rs.getString(2));
            	category.setType(rs.getInt(3));
            	category.setGenDate(rs.getTimestamp(4));
            }
        }
        catch (SQLException e) {
            logger.error("getRow: Error executing SQL " + strSQL + ">>>" + e.toString());
        } finally {
            poolX.releaseConnection(conn, preStmt, rs);
        }
        return category;
    }
    
    /**
     * fucntion get all TennisCategory
     * @return Vector<TennisCategory> 
     * TennisCategory (ID, NAME, TYPE, GEN_DATE)
     */
    public Vector<TennisCategory> findAll() {
    	Connection conn = null;
    	PreparedStatement preStmt = null;
    	ResultSet rs = null;
    	String strSQL = null;
    	TennisCategory category = null;
    	Vector <TennisCategory> vCategory = null;
    	
    	try {
    		conn = poolX.getConnection();
    		strSQL =
    			"select ID, NAME, TYPE, GEN_DATE from TENNIS_CATEGORY ";
    		preStmt = conn.prepareStatement(strSQL);
    		rs = preStmt.executeQuery();
    		vCategory = new Vector<TennisCategory>();
    		while (rs.next()) {
    			category = new TennisCategory();
    			category.setId(rs.getBigDecimal(1));
    			category.setName(rs.getString(2));
    			category.setType(rs.getInt(3));
            	category.setGenDate(rs.getTimestamp(4));
    			
    			vCategory.addElement(category);
    		}
    	}
    	catch (SQLException e) {
    		logger.error("findAll: Error executing SQL " + strSQL + ">>>" + e.toString());
    	} finally {
    		poolX.releaseConnection(conn, preStmt, rs);
    	}
    	return vCategory;
    }
	
    /**
     * function insert a TennisCategory
     * @param name
     * @param type
     * @return true if insert success else return false.
     */
	public boolean insertRow(String name, int type){
		Connection conn = null;
        PreparedStatement preStmt = null;
        boolean result = false;
        String strSQL = null;
        try {
            conn = poolX.getConnection();

        	strSQL =" insert into TENNIS_CATEGORY( ID, NAME, TYPE, GEN_DATE ) " +
    				" values(TENNIS_CATEGORY_SEQ.nextval, ?, ?, sysdate) ";
	        preStmt = conn.prepareStatement(strSQL);
	        preStmt.setString(1, name);
	        preStmt.setInt(2, type);
	        
	        if (preStmt.executeUpdate()>=1) {
	        	result = true;
	        }
        } catch (Exception ex) {
        	logger.error("insertRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
        } finally {
        	poolX.releaseConnection(conn, preStmt);
       }
       return result;
	}
	
	/**
	 * function update TennisCategory
	 * @param id
	 * @param name
	 * @return true if update success else return false.
	 */
	public boolean updateRow(BigDecimal id, String name, int type){
		Connection conn = null;
        PreparedStatement preStmt = null;
        boolean result = false;
        String strSQL = null;
        try {
        	conn = poolX.getConnection();
        	strSQL =" update TENNIS_CATEGORY set NAME = ?, TYPE = ? " +
        			" where ID = ?";
        	preStmt = conn.prepareStatement(strSQL);
        	preStmt.setString(1, name);
        	preStmt.setInt(2, type);
        	preStmt.setBigDecimal(3, id);
        	
        	if(preStmt.executeUpdate() > 0){
        		result = true;
        	}                
        } catch (Exception ex) {
        	logger.error("updateRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
        } finally {
        	poolX.releaseConnection(conn, preStmt);
       }
        return result;
	}	

	/**
	 * function delete a TennisCategory
	 * @param id
	 * @return true if update success else return false.
	 */
	public boolean deleteRow(BigDecimal id){
		Connection conn = null;
		PreparedStatement preStmt = null;
		boolean result = false;
		String strSQL = null;
		try {
			conn = poolX.getConnection();
			strSQL =" delete from TENNIS_CATEGORY where ID = ? ";
			preStmt = conn.prepareStatement(strSQL);
			preStmt.setBigDecimal(1, id);
			if(preStmt.executeUpdate() > 0){
				result = true;
			}
		} catch (Exception ex) {
			logger.error("deleteRow: Error executing SQL " + strSQL + ">>>" + ex.toString());
		} finally {
			poolX.releaseConnection(conn, preStmt);
		}
		return result;
	}
}
